﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;

/// <summary>
/// 采集记录服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class CaiJiLogService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<CaiJiLog> _caiJiLogRep;
    private readonly ISqlSugarClient _db;
    public CaiJiLogService(SqlSugarRepository<CaiJiLog> caiJiLogRep, ISqlSugarClient db)
    {
        _caiJiLogRep = caiJiLogRep;
        _db = db;
    }

    /// <summary>
    /// 分页查询采集记录
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询采集记录")]
    public async Task<SqlSugarPagedList<CaiJiLogOutput>> Page(PageCaiJiLogInput input)
    {
		input.SearchKey = input.SearchKey?.Trim();
        var query = _caiJiLogRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.ClientSn.Contains(input.SearchKey)
                || u.EmpSn.Contains(input.SearchKey)
                || u.EmpName.Contains(input.SearchKey)
                || u.cap_time.Contains(input.SearchKey)
                || u.ImageBase64.Contains(input.SearchKey)
            )
            .WhereIF(input.ProjectId>0, u => u.ProjectId == input.ProjectId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.ClientSn), u => u.ClientSn.Contains(input.ClientSn.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.EmpSn), u => u.EmpSn.Contains(input.EmpSn.Trim()))
            .WhereIF(input.EmployeeId>0, u => u.EmployeeId == input.EmployeeId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.EmpName), u => u.EmpName.Contains(input.EmpName.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.cap_time), u => u.cap_time.Contains(input.cap_time.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.ImageBase64), u => u.ImageBase64.Contains(input.ImageBase64.Trim()))
            .Select<CaiJiLogOutput>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加采集记录
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加采集记录")]
    public async Task<long> Add(AddCaiJiLogInput input)
    {
        var entity = input.Adapt<CaiJiLog>();
        await _caiJiLogRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除采集记录
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除采集记录")]
    public async Task Delete(DeleteCaiJiLogInput input)
    {
        var entity = await _caiJiLogRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _caiJiLogRep.FakeDeleteAsync(entity);   //假删除
        //await _caiJiLogRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 清空采集记录
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "DeleteAll")]
    [DisplayName("清空采集记录")]
    public void DeleteAll()
    {
        //var entity = await _caiJiLogRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //await _caiJiLogRep.FakeDeleteAsync(entity);   //假删除
        //await _caiJiLogRep.DeleteAsync(entity);   //真删除
        //Console.WriteLine("test");
        //_caiJiLogRep
        _db.DbMaintenance.TruncateTable<CaiJiLog>();
    }

    /// <summary>
    /// 更新采集记录
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新采集记录")]
    public async Task Update(UpdateCaiJiLogInput input)
    {
        var entity = input.Adapt<CaiJiLog>();
        await _caiJiLogRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取采集记录
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取采集记录")]
    public async Task<CaiJiLog> Detail([FromQuery] QueryByIdCaiJiLogInput input)
    {
        return await _caiJiLogRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取采集记录列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取采集记录列表")]
    public async Task<List<CaiJiLogOutput>> List([FromQuery] PageCaiJiLogInput input)
    {
        return await _caiJiLogRep.AsQueryable().Select<CaiJiLogOutput>().ToListAsync();
    }





}
